<template>
  <div class="pager-container" v-if="pageNumber > 1">
    <a @click="handleClick(1)" :class="{ disabled: current === 1 }">|&lt;&lt;</a>
    <a @click="handleClick(current - 1)" :class="{ disabled: current === 1 }">&lt;&lt;</a>
    <a @click="handleClick(item)" v-for="(item, index) in numbers" :key="index" :class="{active: item === current}">{{ item }}</a>
    <a @click="handleClick(current + 1)" :class="{ disabled: current === pageNumber }">&gt;&gt;</a>
    <a @click="handleClick(pageNumber)" :class="{ disabled: current === pageNumber }">&gt;&gt;|</a>
  </div>
</template>

<script>
export default {
  props: {
    // 当前页码
    current: {
      type: Number,
      default: () => 1
    },
    // 总数据量
    total: {
      type: Number,
      default: () => 0
    },
    // 页容量
    limit: {
      type: Number,
      default: () => 10
    },
    // 可见页码数
    visibleNumber: {
      type: Number,
      default: () => 10
    }
  },
  computed: {
    pageNumber() {
      return Math.ceil(this.total / this.limit)
    },
    visibleMin() {
      let min = this.current - Math.floor(this.visibleNumber / 2)
      min = Math.max(min, 1)
      return min
    },
    visibleMax() {
      let max = this.visibleMin + (this.visibleNumber - 1)
      max = Math.min(max, this.pageNumber)
      return max
    },
    numbers() {
      let nums = []
      for(let i = this.visibleMin; i <= this.visibleMax; i++) {
        nums.push(i)
      }
      return nums
    }
  },
  methods: {
    handleClick(newPage) {
      this.$emit('pageChange', newPage)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/var.scss';
.pager-container {
  display: flex;
  justify-content: center;
  column-gap: 12px;
  margin: 20px 0;
  a {
    color: $primary;
    cursor: pointer;
    &.disabled {
      color: $lightWords;
      cursor: not-allowed;
    }
    &.active {
      color: $words;
      font-weight: bold;
      cursor: text;
    }
  }
}
</style>